#include <stdio.h>
#include <string.h>
#include <netdb.h>

#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(void)
{
    char port[16];
    snprintf(port, sizeof(port), "%d", 20442);

    struct addrinfo hints, *addr_list, *cur;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    int ret = getaddrinfo("gitee.com", port, &hints, &addr_list);
    printf("getaddrinfo() ret = %d\n", ret);

    struct sockaddr_in *p = NULL;
    char addr[16];
    for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
        p = (struct sockaddr_in *) cur->ai_addr;
        memset(addr, 0x00, sizeof(addr));
        inet_ntop(p->sin_family, &p->sin_addr, addr, sizeof(addr));
        printf("%s:%d\n", addr, htons(p->sin_port));
    }
    return 0;
}
